// Copyright (c) 2009 Mike Haight, Andy Kipp
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "stdafx.h"

AuraParticle::AuraParticle(ParticleSettings^ argSettings) : Particle(argSettings)
{
    mSprite = gcnew Sprite(ResourceManager::GetTexture(Settings->Texture));
    Size = Coordinates::Size((float)mSprite->Texture->FrameWidth, (float)mSprite->Texture->FrameHeight);
}


void AuraParticle::Update(void)
{
    // Get AuraParticleSettings
    AuraParticleSettings^ settings = safe_cast<AuraParticleSettings^>(this->Settings);

    // Update Location
    {
        // Update X Location
        float xDelta = (float)((settings->MaxVelocity.X - settings->MinVelocity.X) * (float)smRandom->NextDouble() + settings->MinVelocity.X);
        Location.X += xDelta;

        // Update Y Location
        float yDelta = (float)((settings->MaxVelocity.Y - settings->MinVelocity.Y) * (float)smRandom->NextDouble() + settings->MinVelocity.Y);
        Location.Y += yDelta;
    }

    // Update Decay
    {
        int decay = smRandom->Next(settings->MinDecay, settings->MaxDecay + 1);
        Life -= decay;
    }

    // Update Sprite
    {
        mSprite->X = Location.X;
        mSprite->Y = Location.Y;
        mSprite->Width = Size.Width;
        mSprite->Height = Size.Height;
    }

}

void AuraParticle::GetSprites(List<Sprite^>^ argSpriteList)
{
    if (Life > 0)
    {
        // Add Sprite to Sprite List
        argSpriteList->Add(mSprite);
    }
}

void AuraParticle::Begin(void)
{
    // Get AuraParticleSettings
    AuraParticleSettings^ settings = safe_cast<AuraParticleSettings^>(this->Settings);

    static float pi = 3.14159f;

    float theta = (float)smRandom->NextDouble() * 2.0f * pi;
    float radius = (float)((settings->MaxRadius - settings->MinRadius) * (float)smRandom->NextDouble() + settings->MinRadius);
    float xDelta = (cosf(theta) * radius);
    float yDelta = (sinf(theta) * radius);

    // Update Location
    {
        Location.X = Origin.X + xDelta;
        Location.Y = Origin.Y + yDelta;
    }

    // Update Frame
    {
        int frame = smRandom->Next(0, 3);
        mSprite->Frame = frame;
    }

    // Set Lifespan for Particle
    Lifespan = smRandom->Next(settings->MinLifespan, settings->MaxLifespan + 1);
    Life = Lifespan;
}